package stuff.hib;

import java.util.List;
import stuff.Country;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import stuff.DictionaryManager;

/**
 *
 * @author Николай
 */
@Repository
@Transactional
public class HibDictionaryManager implements DictionaryManager{

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    @Override
    public void addCoutntries(List<String> cntrs) {
        Session ses = sessionFactory.getCurrentSession();
//        for (String cn : cntrs) {
//            ses.saveOrUpdate(new Country(cn));
//        }
    }

    @Transactional (readOnly=true)
    @Override
    public List<Country> getCountries() {
       return (List<Country>)sessionFactory.
            getCurrentSession().
            createCriteria(Country.class).
            list();
    }

    @Transactional (readOnly=true)
    @Override
    public Country getCountry(String name) {
        return (Country)sessionFactory.getCurrentSession().
            createCriteria(Country.class).
            add(Restrictions.eq("name", name)).
            uniqueResult();
    }
}
